﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Configuration;

namespace ReportForm 
{
    public partial class frmInventory : UTLBASEFORM.MDICHILD
    {
        private static string server = ConfigurationManager.AppSettings["Server"];
        private static string User = ConfigurationManager.AppSettings["UserID"];
        private static string Pass = ConfigurationManager.AppSettings["Password"];
        private static string Database = ConfigurationManager.AppSettings["DataBase"];
        private int type = 1;
        public frmInventory()
        {
            InitializeComponent();
            SetupMenuBar();
            ShowPanel(1);
        }

        private void SetupMenuBar()
        {
            ShowBTN1("Báo Cáo Nhập",null);
            ShowBTN2("Báo Cáo Xuất", null);
            ShowBTN3("Báo Cáo Tồn", null);
            //ShowBTN3("Báo Cáo Tồn", null);
        }
        private void ShowPanel(int id ) {
            if (id == 1)
            {
                panel1.Visible = true;
                panel2.Visible = false;
            }
            else {
                panel1.Visible = false;
                panel2.Visible = true;
            }


        }
        private void LoadCat() {
            Treport tReport = new Treport();
            DataTable dt = tReport.GetCat();
            cmbCat.DataSource = dt;
            cmbCat.DisplayMember="Name";
            cmbCat.ValueMember ="ID";
        }
        private void LoadCat1()
        {
            Treport tReport = new Treport();
            DataTable dt = tReport.GetCat();
            cmdCat1.DataSource = dt;
            cmdCat1.DisplayMember = "Name";
            cmdCat1.ValueMember = "ID";
        }
        private void frmInventory_Load(object sender, EventArgs e)
        {
            SetupMenuBar();
            LoadCat1();
        }
        public override void btn1_Click(object sender, EventArgs e)
        {
            crystalReportViewer1.ReportSource = null;
            ShowPanel(1);
            type = 1;
            
           
        }
        public override void btn2_Click(object sender, EventArgs e)
        {
            panel1.Text = "Báo cáo Xuất";
            crystalReportViewer1.ReportSource = null;
            ShowPanel(1);
            type = 2;
        }
        public override void btn3_Click(object sender, EventArgs e)
        {
            crystalReportViewer1.ReportSource = null;
            ShowPanel(2);

            LoadCat();
        }

        //public void ShowReportBuy(string to , string from) {

        //    string path = Application.StartupPath;
        //    ReportDocument reportDocument = new ReportDocument();
        //    ParameterFields paramFields = new ParameterFields();

        //    ParameterField paramDateFrom = new ParameterField();
        //    ParameterDiscreteValue paramValueFrom = new ParameterDiscreteValue();
        //    ParameterField paramDateTo = new ParameterField();
        //    ParameterDiscreteValue paramValueTo = new ParameterDiscreteValue();


        //    //Set instances for input parameter 1 -  @vDepartment
        //    paramDateFrom.Name = "@DateFrom";
        //    //Below variable can be set to any data present in SalseData table, Department column
        //    paramValueFrom.Value = from;
        //    paramDateFrom.CurrentValues.Add(paramValueFrom);
        //    //Add the paramField to paramFields
        //    paramFields.Add(paramDateFrom);

        //    paramDateTo.Name = "@DateTo";

        //    //Below variable can be set to any data present in SalseData table, Department column
        //    paramValueTo.Value = to;
        //    paramDateTo.CurrentValues.Add(paramValueTo);
        //    //Add the paramField to paramFields
        //    paramFields.Add(paramDateTo);

        //    crystalReportViewer1.ParameterFieldInfo = paramFields;
        //       // ReportDAO rpt = new ReportDAO();
        //    reportDocument.Load(path + "\\rpBuy.rpt");
        //    //  rpt.kenoi(reportDocument);

        //    reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);
        //    TableLogOnInfo logonInfo = new TableLogOnInfo();
        //    foreach (Table table in reportDocument.Database.Tables)
        //    {
        //        logonInfo = table.LogOnInfo;
        //        logonInfo.ConnectionInfo.ServerName = server;
        //        logonInfo.ConnectionInfo.UserID = User;
        //        logonInfo.ConnectionInfo.Password = Pass;
        //        logonInfo.ConnectionInfo.DatabaseName = Database;
        //        table.ApplyLogOnInfo(logonInfo);
        //    }
        //    //set the database loggon information.
        //    //**Note that the third parameter is the DSN name and not the Database or System name

        //    //  reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);

        //    // reportDocument.SetDatabaseLogon("sa", "tqtrang07", "TQTRANG", "hongphucgoldDB", false);
        //    //Load the report by setting the report source

        //    crystalReportViewer1.ReportSource = reportDocument;
        //}
        
        //public void ShowReportBuySell( int idCat,string to, string from)
        //{

        //    string path = Application.StartupPath;
        //    ReportDocument reportDocument = new ReportDocument();
        //    ParameterFields paramFields = new ParameterFields();
            
        //    ParameterField paramidCat = new ParameterField();
        //    ParameterDiscreteValue paramValueidCat = new ParameterDiscreteValue();
        //    ParameterField paramDateFrom = new ParameterField();
        //    ParameterDiscreteValue paramValueFrom = new ParameterDiscreteValue();
        //    ParameterField paramDateTo = new ParameterField();
        //    ParameterDiscreteValue paramValueTo = new ParameterDiscreteValue();

        //    //Set instances for input parameter 1 -  @vDepartment
        //    paramidCat.Name = "@idcat";
        //    //Below variable can be set to any data present in SalseData table, Department column
        //    paramValueidCat.Value = idCat;
        //    paramidCat.CurrentValues.Add(paramValueidCat);
        //    //Add the paramField to paramFields
        //    paramFields.Add(paramidCat);

        //    //Set instances for input parameter 1 -  @vDepartment
        //    paramDateFrom.Name = "@tungay";
        //    //Below variable can be set to any data present in SalseData table, Department column
        //    paramValueFrom.Value = DateTime.Parse(from);
        //    paramDateFrom.CurrentValues.Add(paramValueFrom);
        //    //Add the paramField to paramFields
        //    paramFields.Add(paramDateFrom);

        //    paramDateTo.Name = "@denngay";

        //    //Below variable can be set to any data present in SalseData table, Department column
        //    paramValueTo.Value = DateTime.Parse(to);
        //    paramDateTo.CurrentValues.Add(paramValueTo);
        //    //Add the paramField to paramFields
        //    paramFields.Add(paramDateTo);

        //    crystalReportViewer1.ParameterFieldInfo = paramFields;
        //    // ReportDAO rpt = new ReportDAO();
        //    reportDocument.Load(path + "\\BuySell.rpt");
        //    //  rpt.kenoi(reportDocument);

        //    reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);
        //    TableLogOnInfo logonInfo = new TableLogOnInfo();
        //    foreach (Table table in reportDocument.Database.Tables)
        //    {
        //        logonInfo = table.LogOnInfo;
        //        logonInfo.ConnectionInfo.ServerName = server;
        //        logonInfo.ConnectionInfo.UserID = User;
        //        logonInfo.ConnectionInfo.Password = Pass;
        //        logonInfo.ConnectionInfo.DatabaseName = Database;
        //        table.ApplyLogOnInfo(logonInfo);
        //    }
        //    //set the database loggon information.
        //    //**Note that the third parameter is the DSN name and not the Database or System name

        //    //  reportDocument.SetDatabaseLogon(User, Pass, server, Database, true);

        //    // reportDocument.SetDatabaseLogon("sa", "tqtrang07", "TQTRANG", "hongphucgoldDB", false);
        //    //Load the report by setting the report source

        //    crystalReportViewer1.ReportSource = reportDocument;
        //}
        private void cmdBuyReport_Click(object sender, EventArgs e)
        {

          //  ShowReportBuy(dtkTo.Value.ToShortDateString(), dtkFrom.Value.ToShortDateString());
          if(type==1)
            ShowBuy(int.Parse(cmdCat1.SelectedValue.ToString()),dtkTo.Value.ToShortDateString(), dtkFrom.Value.ToShortDateString());
            else
              ShowSell(int.Parse(cmdCat1.SelectedValue.ToString()),dtkTo.Value.ToShortDateString(), dtkFrom.Value.ToShortDateString());
        }

        private void cmdReportBuySell_Click(object sender, EventArgs e)
        {
           
          //  ShowReportBuySell(int.Parse(cmbCat.SelectedValue.ToString()), dpkFromBuySell.Value.ToShortDateString(), dpkToBuySell.Value.ToShortDateString());
            ShowStock(int.Parse(cmbCat.SelectedValue.ToString()), dpkFromBuySell.Value.ToShortDateString(), dpkToBuySell.Value.ToShortDateString());
        }

        private void ShowStock(int id, string from, string to) {
            BLL.tReportStock tStock = new BLL.tReportStock();
            DataSet ds = new DataSet();
            DataTable dt = tStock.SetData(id,from,to);
            ds.Tables.Add(dt);
            rptStock rpt = new rptStock();
            //  rptStamp rpt = new rptStamp();
            rpt.SetDataSource(ds.Tables[0]);
            crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
        }

        private void ShowBuy(int idCat, string from, string to)
        {
            BLL.tReportBuy tBuy= new BLL.tReportBuy();
            DataSet ds = new DataSet();
            DataTable dt = tBuy.SetData(idCat,from, to);
            ds.Tables.Add(dt);
            rptBuyNew rpt = new rptBuyNew();
            //  rptStamp rpt = new rptStamp();
            rpt.SetDataSource(ds.Tables[0]);
            crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
        }
        private void ShowSell( int idCat,string from, string to)
        {
            BLL.tReportSell tSell = new BLL.tReportSell();
            DataSet ds = new DataSet();
            DataTable dt = tSell.SetData(idCat,from, to);
            ds.Tables.Add(dt);
            rptSell rpt = new rptSell();
            //  rptStamp rpt = new rptStamp();
            rpt.SetDataSource(ds.Tables[0]);
            crystalReportViewer1.ReportSource = rpt;
            crystalReportViewer1.Refresh();
        }

     
    }
}
